﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using TfsGiggle.Core;
using System.Threading.Tasks;
using System.Diagnostics;

namespace TfsGiggle.Search
{
    /// <summary>
    /// Interaction logic for TfsGiggleSearchControl.xaml
    /// </summary>
    public partial class TfsGiggleSearchControl : UserControl
    {
        private ObservableCollection<TfsGiggleFile> _tfsGiggleFiles = new ObservableCollection<TfsGiggleFile>();

        /// <summary>
        /// Control
        /// </summary>
        public TfsGiggleSearchControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The search results
        /// </summary>
        public ObservableCollection<TfsGiggleFile> TfsGiggleFiles
        {
            get
            {
                return _tfsGiggleFiles;
            }
        }

        /// <summary>
        /// Open the document
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ListViewHandleDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var file = (TfsGiggleFile)((ListViewItem)sender).Content;
                var filePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(file.Name));
                TfsGiggleSearchPackage.Instance.ShowFile(file.TeamProjectCollectionId, file.Name);
            }
            catch (Exception ex)
            {
                ActivityLog.LogError("Exception caught in TfsGiggle", ex.ToString());
                throw;
            }
        }
    }
}
